<template>
  <div class="layout-container">
    <div class="back-header">
      <el-page-header title="返回" :content="pageHeader" @back="goBack" />
    </div>
    <div class="flex-start-nowrap pl30" v-if="query.type == 1">
      <el-radio-group v-model="tabList.value" @change="chagneTab" >
        <el-radio-button 
          :label="item.value"
          v-for="(item, index) in tabList.list" 
          :key="index">{{item.label}}</el-radio-button>
      </el-radio-group>
    </div>
    <el-form
      ref="refruleForm"
      :model="form"
      :rules="rules"
      label-width="130px"
      style=""
      class="m-form"
    >
      <template v-if="query.type ==2">
        
        <el-form-item label="项目权限" prop="project_id" style="width:500px">
          <el-select
            v-model="form.project_id"
            filterable
            placeholder="请选择项目权限"
            :filter-method="remoteMethod"
            style="width: 100%"
          >
            <el-option
              v-for="item in authOptions"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
      </template>
      <template v-if="query.type == 1">
         <el-form-item label="通知团队"  prop="project_id" style="width:500px" v-if="tabList.value == 1">
          <el-select
            v-model="form.project_id"
            multiple clearable
            filterable
             @change="changeSelect"
            :remote-method="remoteMethodTeam"
            placeholder="请选择项目权限"
            style="width: 100%"
          >
             <!-- 
            remote reserve-keyword
            -->
            <el-option
              v-for="item in authOptions"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="通知个人" label-width="130px" prop="user" style="width:500px" v-else>
          <div class="flex-start-nowrap">
            <el-input v-model="form.user" placeholder="请输入用户ID/邮箱/手机号"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="通知形式" label-width="130px" prop="msg_way" style="width:500px">
          <div class="flex-start-nowrap">
            <!-- <el-checkbox label="系统内通知"></el-checkbox> -->
            <el-radio-group v-model="form.msg_way" class="ml-4" :class="['el-radio-group-me']">
              <el-radio label="1" size="large">系统内通知</el-radio>
              <el-radio label="2" size="large">邮件通知</el-radio>
              <el-radio label="3" size="large">手机短信通知</el-radio>
            </el-radio-group>
          </div>
        </el-form-item>
        <!-- <el-form-item label="" label-width="130px"  prop="project_id" style="width:500px">
          <div class="flex-start-nowrap">
            <el-checkbox label="邮件通知"></el-checkbox>
          </div>
        </el-form-item>
        <el-form-item label="" label-width="130px" prop="project_id" style="width:500px">
          <div class="flex-start-nowrap">
            <el-checkbox label="手机短信通知"></el-checkbox>
          </div>
        </el-form-item> -->
      </template>
      <el-form-item label="通知内容：" prop="content"  style="width:90%">
       <!-- <editor v-model="myValue" :init="init" :disabled="disabled" :id="tinymceId"></editor> -->
        <!-- <el-input
          type="textarea"
          v-model="form.content"
          placeholder="请输入通知内容"
          rows="5"
          maxlength="200"
          show-word-limit
        /> -->
        <WangEditor ref="WangEditors" :modelValue="form.content"></WangEditor>
      </el-form-item>
      
      <el-form-item class="flex-end">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" v-loading="submitLoading" @click="handleSubmit"
          >确认发送</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
<script setup lang="ts">
import {ref, reactive } from "vue";
import { defineComponent , ElMessage, ElMessageBox } from "element-plus";
import { useRouter, useRoute } from "vue-router";
import { sendGlobal,sendProject } from "@/api/notice.ts";
import { getAuthList } from "@/api/service/user.ts";
import { teamPage } from "@/api/team.ts";
import WangEditor from "@/components/tinymce1/index.vue";
const router = useRouter();
const route = useRoute();
const pageHeader = ref("");
const query = ref({...route.query});
const dataObj = reactive({
  1:{
   title:'新建全局通知' ,
   type:1
  },
  2:{
   title:'新建项目通知' ,
   type:2,
  },

})


const tabList = reactive({
  value:1,
  list:[
    {
      label:'团队通知',
      value:1
    }, {
      label:'个人通知',
      value:2
    }
  ]
})
const chagneTab = (e:any) =>{
  isAll.value = false
}

const isAll = ref(false)
const changeSelect = (e:any) =>{
  isAll.value = false;
  let len = 0
  e.map(el =>{return len+=1})
  let isAlling = len == authOptions.value.length
  let num = 0
  e.some(item => {
    if(item == '全部团队'){
      num = 1
      return true
    }
  })
  if(num){
    form.project_id = []
    if(!isAlling){
      authOptions.value.map(item => {
        if(item != '全部团队'){
          form.project_id.push(item)
        }
      })
    }
    if(isAlling){
      isAll.value = false;
    }else{
      isAll.value = true;
    }
  }
}

// 获取项目权限
const authOptions = ref([]);
const getAuthListFun = (search_text: String) => {
  let data = {
    search_text: search_text,
    tp: 0 // 0：项目 1：团队
  }
  getAuthList(data).then((res: any) => {
    if (res.code === 200) {
      authOptions.value = res.data
    }
  })
}

const remoteMethod = (val: any) => {
  getAuthListFun(val);
}



const teamOptions = ref([]);
const getTeamOptions = (search_text: String) => {
  let data = {
    page:1,
    page_size:999,
  }
  teamPage(data).then((res: any) => {
    if (res.code === 200) {

      res.data.list = res.data.list.map(item => {return item.team_number})
      res.data.list.unshift('全部团队')
      res.data.list.push('zxz')
      authOptions.value = res.data.list

    }
  })
}
pageHeader.value = dataObj[query.value.type].title
if(query.value.type == 1){
  
  getTeamOptions('');
}else{
  getAuthListFun('');
}

const remoteMethodTeam = (val: any) => {
  getTeamOptions(val);
}





const refruleForm: any = ref(null);
const form: any = reactive({
  content: "", // string 分类大图
  project_id: "",
});

// 必填校验
const rules = reactive({
  content: [
    {
      required: true,
      message: "请输入通知内容",
      trigger: "blur",
    },
  ], user: [
    {
      required: true,
      message: "请输入用户信息",
      trigger: "blur",
    },
  ],
  project_id: [
    {
      required: true,
      message: "请选择权限",
      trigger: "blur",
    },
  ],msg_way: [
    {
      required: true,
      message: "请选择通知形式",
      trigger: "blur",
    },
  ],
});

// 返回
const goBack = () => {
  router.push({
    path: "/backstagerpzzm/notificationMessage/notificationMessage",
  });
};
// 取消
const handleCancel = () => {
  router.push({
    path: "/backstagerpzzm/notificationMessage/notificationMessage",
  });
};
const WangEditors = ref()
// 确认发送

const handleSubmit = () => {
  form.content = WangEditors.value.getContent()
  if(!form.content){
    ElMessage.error("请填写内容！");
    return 
  }
  refruleForm.value.validate((valid: Boolean) => {
    if (valid) {
      sendMessageFun()
    } else {
      console.log("error submit!!");
      return false;
    }
  });
};
const submitLoading = ref(false)
// 消息发送事件
const sendMessageFun = () => {
  debugger
  submitLoading.value = true;
  let data = {
    content: form.content,
  }
  if(query.value.type == 1){
    data.msg_type = isAll.value ? 1:tabList.value == 1?2:3
    data.msg_way = form.msg_way
  }else{
    data.permissions = form.project_id
  }
  if(data.msg_type == 2){
    data.team_number =isAll.value? [] : form.project_id.join(',')
  }
  if(data.msg_type == 3){
    data.user = form.user
  }
  setTimeout(()=>{
     submitLoading.value = false
  },10000)
  if(query.value.type == 1){
    sendGlobal(data).then((res: any) => {
      if (res.code === 200) {
        submitLoading.value = false
        ElMessage.success("发送成功！");
        goBack()
        
      }else{
         submitLoading.value = false
      }
    })
  }else{
    sendProject(data).then((res: any) => {
      if (res.code === 200) {
         submitLoading.value = false
        ElMessage.success("发送成功！");
        goBack()
        
      }else{
         submitLoading.value = false
      }
    })
  }

  
}
</script>

<style lang="scss" >
.pl30{
  padding-left: 130px;
  margin-bottom: 20px;
}
.layout-container {
  height: auto;
  .back-header {
    padding: 20px;
    // width: 100%;
    border-bottom: 1px solid #fafafa;
  }
  
}
.m-form{
  .el-select {
    .el-select__tags>span{
      max-height: 400px;
      overflow-y: auto;
    }
   
  }
}
.flex-end{
  width: 90%;
  .el-form-item__content{
    display: flex;
    justify-content: flex-end;
  }
 
}
.flex-start-nowrap{
    display: flex;
    justify-content: flex-start;
    flex-wrap: nowrap;
}
.el-radio-group-me{
  flex-direction: column;
  display: flex;
  .el-radio{
    display: flex;
    justify-content: flex-start;
    margin-right: 0;
    margin-bottom: 20px;
    align-items: center;
    padding-top: 5px;
  }
}
</style>
